package com.luo.ai.assistant.chat.tool;

import com.luo.ai.assistant.util.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.model.ToolContext;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

/**
 * Tool for chat
 *
 * @author luohq
 * @date 2025-02-11 8:56
 * @see  org.springframework.ai.tool.method.MethodToolCallbackProvider
 * @see org.springframework.ai.util.json.schema.JsonSchemaGenerator#generateForMethodInput
 * @see org.springframework.ai.tool.method.MethodToolCallback
 * @see org.springframework.ai.tool.ToolCallbacks
 */
@Component
public class MyChatTools {
    private Logger log = LoggerFactory.getLogger(MyChatTools.class);

    @Tool(description = "根据PlanetUML脚本生成预览图，并返回预览图URL")
    String generatePlanetUmlPreviewImage(@ToolParam(description = "PlanetUML脚本", required = true) String planetUmlScript) {
        log.debug("[Tool] Generate PlanetUML preview image for script: {}", planetUmlScript);
        return "https://www.plantuml.com/plantuml/png/PP3FQnD17CNlyrUiYuSD27heKQLh33LGJ8aLyRHCpQ0BixDXPnOQH4ZK_gY96Z6LDWL3mM0zc2ruiBLn-yUuCxFvB_okQqrgvpQFztbytvld0WSYz2YwvB97DAmHutfnxcfnpYdwJ91LSHEXHVjXvVwoyw2yVCTPB2sKxjws2WW9Lr1Ys6NvwelQQYT7RVLXG-x529M02-g1oubpomzP3GSDYwnSksAOoRYL7FNJ-5XlTyo2yHGPy9P8m7rcUPZXnoG0bnxjw-wwVBTZPfzl9_faC9KAiqp8_m1gTpHfzdNlG6r-ds7wBvEVfw2cpRM_sBq65yIpi6jpxa9n9i1zrY0ZLIzIdPQCZjMReVpqMHtiou_jNyrMrLw0Y6S8vGbEQLLOb7evVURB0Yfrmkmo49ETqyb6H-_6-laOlXLAiOVrroqmNUOmZY0tpC1VyGNamiAuuLId4sIz2gXAw5GwVxECnd9jgBOFrUiHfElUG6rszM2OHklon_lia2T2rBcPXuJyOa8-pV_ESvwyO7hu0ep_IggttIJUWstjfV8iFKzOBVJeRm00";
    }

    //工具 - 获取当前时区的时间
    @Tool(description = "Get the current date and time in the user's timezone")
    String currentDateTime() {
        String curDateTime = LocalDateTime.now().atZone(LocaleContextHolder.getTimeZone().toZoneId()).toString();
        log.debug("[Tool] current date time: {}", curDateTime);
        return curDateTime;
    }

    /**
     * 用户ID - 上下文参数名
     */
    public static final String USER_ID_PARAM = "userId";

    //工具 - 根据指定时间设置报警
    @Tool(description = "Set a user alarm for the given time")
    void setAlarm(@ToolParam(description = "Time in ISO-8601 format") String time, ToolContext toolContext) {
        //获取上下文参数
        Object userIdObj = toolContext.getContext().get(USER_ID_PARAM);
        //转换参数类型
        String userId = String.valueOf(userIdObj);

        LocalDateTime alarmTime = LocalDateTime.parse(time, DateTimeFormatter.ISO_DATE_TIME);
        log.debug("[Tool] Alarm set for user: {}, time: {}", userId, alarmTime);
    }


    //@Tool(description = "根据用户ID查询用户详情，返回信息包括用户标识userId，用户账号userAccount，用户名userName。")
    @Tool(description = "根据用户ID查询用户详情")
    Map<String, Object> getUserInfo(@ToolParam(description = "用户ID") String userId) {
        Map<String, Object> userInfo = Map.of("userId", userId,
                "userAccount", "luohq",
                "userName", "罗斯特洛夫斯基",
                "createTime", "2024-12-11 12:53:23");
        log.debug("[Tool] Get user info by userId {}: {}", userId, JsonUtils.toJson(userInfo));
        return userInfo;
    }


    @Tool(description = "根据用户ID查询用户关联的角色列表")
    List<Map<String, Object>> getUserRoles(@ToolParam(description = "用户ID") String userId) {
        List<Map<String, Object>> roleList = List.of(
                Map.of("roleCode", "admin",
                        "roleName", "管理员",
                        "createTime", "2025-01-11 08:56:33"),
                Map.of("roleCode", "user",
                        "roleName", "普通用户",
                        "createTime", "2025-02-22 13:16:02")
        );
        log.debug("[Tool] Get roles for user {}: {}", userId, JsonUtils.toJson(roleList));
        return roleList;
    }

}
